﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace QExplorer
{
	class BookmarkBar : ToolStrip
	{
		private ContextMenuStrip contextMenuStrip1;
		private System.ComponentModel.IContainer components;
		private ToolStripMenuItem toolStripMenuItemDel;
		private QForm form;
		public QExplorer.QForm Form
		{
			get { return form; }
			set { form = value; }
		}
		private ToolStripItem clickedItem; 

		public BookmarkBar()
			:base()
		{
			InitializeComponent();
		}

		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
			this.toolStripMenuItemDel = new System.Windows.Forms.ToolStripMenuItem();
			this.contextMenuStrip1.SuspendLayout();
			this.SuspendLayout();
			// 
			// contextMenuStrip1
			// 
			this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripMenuItemDel});
			this.contextMenuStrip1.Name = "contextMenuStrip1";
			this.contextMenuStrip1.ShowImageMargin = false;
			this.contextMenuStrip1.Size = new System.Drawing.Size(85, 26);
			// 
			// toolStripMenuItemDel
			// 
			this.toolStripMenuItemDel.Name = "toolStripMenuItemDel";
			this.toolStripMenuItemDel.Size = new System.Drawing.Size(84, 22);
			this.toolStripMenuItemDel.Text = "&Delete";
			this.toolStripMenuItemDel.Click += new System.EventHandler(this.toolStripMenuItemDel_Click);
			// 
			// BookmarkBar
			// 
			this.AllowDrop = true;
			this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.BookmarkBar_MouseClick);
			this.DragOver += new System.Windows.Forms.DragEventHandler(this.BookmarkBar_DragOver);
			this.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.BookmarkBar_ItemClicked);
			this.DragEnter += new System.Windows.Forms.DragEventHandler(this.BookmarkBar_DragEnter);
			this.GiveFeedback += new System.Windows.Forms.GiveFeedbackEventHandler(this.BookmarkBar_GiveFeedback);
			this.DragDrop += new System.Windows.Forms.DragEventHandler(this.BookmarkBar_DragDrop);
			this.contextMenuStrip1.ResumeLayout(false);
			this.ResumeLayout(false);

		}

		void toolStripMenuItemDel_Click(object sender, EventArgs e)
		{
			if (clickedItem == null)
			{
				return;
			}
			Items.Remove(clickedItem);
		}

		private void BookmarkBar_DragDrop(object sender, DragEventArgs e)
		{
			string path = (string)e.Data.GetData(typeof(string));
			AddBookmark(path);
		}

		private void BookmarkBar_DragEnter(object sender, DragEventArgs e)
		{
			e.Effect = DragDropEffects.Link;
		}

		private void BookmarkBar_DragOver(object sender, DragEventArgs e)
		{

		}

		public void AddBookmark(string path)
		{
			foreach (ToolStripItem item in Items)
			{
				if ((string)item.Tag == path)
				{
					return;
				}
			}
			Icon icon;
			ToolStripButton btn = new ToolStripButton();
			Items.Add(btn);
			SHFILEINFO sfi = new SHFILEINFO();
			API.GetFileInfo(path, ref sfi, API.SHGFI_DISPLAYNAME);
			btn.Text = sfi.szDisplayName;
			if (path == Environment.GetFolderPath(Environment.SpecialFolder.Desktop))
			{
				icon = API.GetIcon(API.CSIDL_DESKTOP);
			}
			else if (path == Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
			{
				icon = API.GetIcon(API.CSIDL_PERSONAL);
			}
			else
			{
				API.GetFileInfo(path, ref sfi, API.SHGFI_ICON | API.SHGFI_SMALLICON);
				icon = Icon.FromHandle(sfi.hIcon);
			}
			btn.Image = icon.ToBitmap();
			btn.DisplayStyle = ToolStripItemDisplayStyle.Image;
			btn.Tag = path;
			btn.AllowDrop = true;
			btn.GiveFeedback += new GiveFeedbackEventHandler(btn_GiveFeedback);
			btn.QueryContinueDrag += new QueryContinueDragEventHandler(btn_QueryContinueDrag);
		}

		void btn_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
		{
			MessageBox.Show("djk");
			throw new NotImplementedException();
		}

		void btn_GiveFeedback(object sender, GiveFeedbackEventArgs e)
		{
			throw new NotImplementedException();
		}

		private void BookmarkBar_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
		{
			ToolStripButton btn = (ToolStripButton)e.ClickedItem;
			if (btn.Tag != null)
			{
				form.Open((string)btn.Tag);
			}
		}

		private void BookmarkBar_GiveFeedback(object sender, GiveFeedbackEventArgs e)
		{
			
		}

		private void BookmarkBar_MouseClick(object sender, MouseEventArgs e)
		{
			if (e.Button != MouseButtons.Right)
			{
				return;
			}
			ToolStripItem btn = (ToolStripItem)GetItemAt(e.X, e.Y);
			if (btn == null)
			{
				return;
			}
			contextMenuStrip1.Show(this, new Point(e.X, e.Y), ToolStripDropDownDirection.BelowRight);
			clickedItem = btn;
		}
	}
}
